﻿using System;

using ApplicationLibrary;
using ApplicationLibrary.SqlConnector;
using System.Windows.Forms;
using ApplicationLibrary.Preferences;
using Plumber.Masters.ModuleData;
using Plumber.Sales.Preferences;

namespace Plumber.Sales.ModuleData
{
    internal class RegisterModule : ApplicationLibrary.SqlConnector.RegisterModule
    {
        public override string Application()
        {
            return "PLMB";
        }

        public override string Module()
        {
            return "SALE";
        }

        public override int CurrentVersion()
        {
            return 10;
        }

        public override Version DllVersion
        {
            get
            {
                return System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
            }
        }

        public override void RegisterCountersAndCodes()
        {
            GlobalInfo.AddCounter((int)Counters.SaleInvoice, Properties.Resources.CN_SaleInvoices);
            GlobalInfo.AddCounter((int)Counters.Quotations, Properties.Resources.CN_Quotations);
            GlobalInfo.AddCounter((int)Counters.DebitNoteEmitted, Properties.Resources.CN_DebitNoteEmitted);
            GlobalInfo.AddCounter((int)Counters.CreditNoteEmitted, Properties.Resources.CN_CreditNoteEmitted);
        }

        public override void AddScrounger(ApplicationLibrary.DBManager.iScrounger frm, NameSpace nameSpace)
        {
            if (nameSpace.ApplicationPath == "Plumber.Masters.Forms.Masters")
                frm.ScroungerList.Add(new Plumber.Sales.Scrounger.MasterScrounger(frm));
        }

        public override ApplicationLibrary.Preferences.PreferencePanel[] RegisterPreferences()
        {
            return new ApplicationLibrary.Preferences.PreferencePanel[] 
            {
                new SalesPreferencePanel("")
            };
        }

        protected override bool CreateDBTables()
        {
            SqlCreateTable newTable = new SqlCreateTable(SqlABConnection);

            #region PL_SalesInvoices

            if (SearchTable(PL_SalesInvoices.Name))
            {
                newTable.NewTable(PL_SalesInvoices.Name);
                newTable.AddColumn(PL_SalesInvoices.InvoiceType);
                newTable.AddColumn(PL_SalesInvoices.InvoiceNo);
                newTable.AddColumn(PL_SalesInvoices.ReleaseDate);
                newTable.AddColumn(PL_SalesInvoices.Type);
                newTable.AddColumn(PL_SalesInvoices.Master);
                newTable.AddColumn(PL_SalesInvoices.Title);
                newTable.AddColumn(PL_SalesInvoices.Description);
                newTable.AddColumn(PL_SalesInvoices.Address);
                newTable.AddColumn(PL_SalesInvoices.ZipCode);
                newTable.AddColumn(PL_SalesInvoices.City);
                newTable.AddColumn(PL_SalesInvoices.County);
                newTable.AddColumn(PL_SalesInvoices.TaxNo);
                newTable.AddColumn(PL_SalesInvoices.FiscalCode);
                newTable.AddColumn(PL_SalesInvoices.Administrator);
                newTable.AddColumn(PL_SalesInvoices.InvoiceBody);
                newTable.AddColumn(PL_SalesInvoices.Amount);
                newTable.AddColumn(PL_SalesInvoices.Total);
                newTable.AddColumn(PL_SalesInvoices.Tax);
                newTable.AddColumn(PL_SalesInvoices.TaxCode);

                newTable.AddColumn(PL_SalesInvoices.PaymentDate);
                newTable.AddColumn(PL_SalesInvoices.SalePayed);
                newTable.AddColumn(PL_SalesInvoices.AmountPayed);

                newTable.AddColumn(PL_SalesInvoices.PrintEnvelope);

                newTable.AddColumn(PL_SalesInvoices.Restraint);
                newTable.AddColumn(PL_SalesInvoices.RestraintCode);
                newTable.AddColumn(PL_SalesInvoices.RestraintAmount);

                newTable.AddColumn(PL_SalesInvoices.Deduction);
                newTable.AddColumn(PL_SalesInvoices.DeductionAmount);
                newTable.AddColumn(PL_SalesInvoices.BankRestraint);
                newTable.AddColumn(PL_SalesInvoices.BankRestraintAmount);

                newTable.AddColumn(PL_SalesInvoices.InvoiceRef);

                newTable.AddPrimaryKey(PL_SalesInvoices.InvoiceType, PL_SalesInvoices.InvoiceNo);

                newTable.Create();
            }

            #endregion

            #region PL_Sentences

            if (SearchTable(PL_Sentences.Name))
            {
                newTable.NewTable(PL_Sentences.Name);

                newTable.AddColumn(PL_Sentences.ID);
                newTable.AddColumn(PL_Sentences.Title);
                newTable.AddColumn(PL_Sentences.Text);

                newTable.AddPrimaryKey(PL_Sentences.ID);

                newTable.Create();
            }

            #endregion

            #region PL_Templates

            if (SearchTable(PL_Templates.Name))
            {
                newTable.NewTable(PL_Templates.Name);

                newTable.AddColumn(PL_Templates.Code);
                newTable.AddColumn(PL_Templates.Description);
                newTable.AddColumn(PL_Templates.Body);
                newTable.AddColumn(PL_Templates.Combine);

                newTable.AddPrimaryKey(PL_Templates.Code);

                newTable.Create();
            }

            #endregion

            #region PL_Quotations

            if (SearchTable(PL_Quotations.Name))
            {
                newTable.NewTable(PL_Quotations.Name);
                newTable.AddColumn(PL_Quotations.QuotationNo);
                newTable.AddColumn(PL_Quotations.ReleaseDate);
                newTable.AddColumn(PL_Quotations.Type);
                newTable.AddColumn(PL_Quotations.Master);
                newTable.AddColumn(PL_Quotations.Title);
                newTable.AddColumn(PL_Quotations.Description);
                newTable.AddColumn(PL_Quotations.Address);
                newTable.AddColumn(PL_Quotations.ZipCode);
                newTable.AddColumn(PL_Quotations.City);
                newTable.AddColumn(PL_Quotations.County);
                newTable.AddColumn(PL_Quotations.TaxNo);
                newTable.AddColumn(PL_Quotations.FiscalCode);
                newTable.AddColumn(PL_Quotations.Administrator);
                newTable.AddColumn(PL_Quotations.QuotationBody);
                newTable.AddColumn(PL_Quotations.Note);

                newTable.AddPrimaryKey(PL_Quotations.QuotationNo);

                newTable.Create();
            }

            #endregion

            return true;
        }

        protected override bool UpdateDBTables()
        {
            if (dbVersion < 3)
            {
                SqlCreateTable alterMaster = new SqlCreateTable(SqlABConnection);

                if (!SearchColumn(PL_SalesInvoices.AmountPayed))
                {
                    alterMaster.AlterTable(PL_SalesInvoices.Name);
                    alterMaster.AddColumn(PL_SalesInvoices.AmountPayed);
                    alterMaster.Alter();
                }
            }

            if (dbVersion < 4)
            {
                SqlCreateTable alterMaster = new SqlCreateTable(SqlABConnection);

                if (!SearchColumn(PL_SalesInvoices.Deduction))
                {
                    alterMaster.AlterTable(PL_SalesInvoices.Name);
                    alterMaster.AddColumn(PL_SalesInvoices.Deduction);
                    alterMaster.Alter();

                    QueryBuilder qb = new QueryBuilder(false, true);
                    qb.Update(PL_SalesInvoices.Name);
                    qb.AddSet<bool>(PL_SalesInvoices.Deduction, false);
                    qb.AddCompareIsNull(PL_SalesInvoices.Deduction);
                    int nRow = qb.ExecuteNonQuery();
                }
            }

            if (dbVersion < 5)
            {
                SqlCreateTable alterMaster = new SqlCreateTable(SqlABConnection);

                if (!SearchColumn(PL_Templates.Type))
                {

                    alterMaster.AlterTable(PL_Templates.Name);
                    alterMaster.AddColumn(PL_Templates.Type);
                    alterMaster.Alter();

                    QueryBuilder qb = new QueryBuilder(false, true);
                    qb.Update(PL_Templates.Name);
                    qb.AddSet<int>(PL_Templates.Type, 0);
                    qb.AddCompareIsNull(PL_Templates.Type);
                    int nRow = qb.ExecuteNonQuery();

                    alterMaster.AlterTable(PL_Templates.Name);
                    alterMaster.DropPrimaryKey(PL_Templates.Name);
                    alterMaster.Alter();

                    alterMaster.AlterTable(PL_Templates.Name);
                    alterMaster.AlterAddPrimaryKey(PL_Templates.Type, PL_Templates.Code);
                    alterMaster.Alter();
                }
            }

            if (dbVersion < 6)
            {
                SqlCreateTable alterMaster = new SqlCreateTable(SqlABConnection);

                if (!SearchColumn(PL_SalesInvoices.InvoiceType))
                {

                    alterMaster.AlterTable(PL_SalesInvoices.Name);
                    alterMaster.AddColumn(PL_SalesInvoices.InvoiceType);
                    alterMaster.Alter();

                    QueryBuilder qb = new QueryBuilder(false, true);
                    qb.Update(PL_SalesInvoices.Name);
                    qb.AddSet<int>(PL_SalesInvoices.InvoiceType, InvoiceType.E_InvoiceEm.Int());
                    qb.AddCompareIsNull(PL_SalesInvoices.InvoiceType);
                    int nRow = qb.ExecuteNonQuery();
                }
            }

            if (dbVersion < 7)
            {
                SqlCreateTable alterMaster = new SqlCreateTable(SqlABConnection);

                if (!SearchColumn(PL_SalesInvoices.BankRestraint))
                {
                    alterMaster.AlterTable(PL_SalesInvoices.Name);
                    alterMaster.AddColumn(PL_SalesInvoices.BankRestraint);
                    alterMaster.Alter();

                    QueryBuilder qb = new QueryBuilder(false, true);
                    qb.Update(PL_SalesInvoices.Name);
                    qb.AddSet<bool>(PL_SalesInvoices.BankRestraint, false);
                    qb.AddCompareIsNull(PL_SalesInvoices.BankRestraint);
                    int nRow = qb.ExecuteNonQuery();
                }
            }

            if (dbVersion < 8)
            {
                SqlCreateTable alterMaster = new SqlCreateTable(SqlABConnection);

                if (!SearchColumn(PL_SalesInvoices.InvoiceRef))
                {
                    alterMaster.AlterTable(PL_SalesInvoices.Name);
                    alterMaster.AddColumn(PL_SalesInvoices.InvoiceRef);
                    alterMaster.Alter();

                    QueryBuilder qb = new QueryBuilder(false, true);
                    qb.Update(PL_SalesInvoices.Name);
                    qb.AddSet<string>(PL_SalesInvoices.InvoiceRef, "");
                    qb.AddCompareIsNull(PL_SalesInvoices.InvoiceRef);
                    int nRow = qb.ExecuteNonQuery();
                }
            }

            if (dbVersion < 9)
            {
                SqlCreateTable alterMaster = new SqlCreateTable(SqlABConnection);


                alterMaster.AlterTable(PL_SalesInvoices.Name);
                alterMaster.AlterColumn(PL_SalesInvoices.InvoiceType);
                alterMaster.Alter();

                alterMaster.AlterTable(PL_SalesInvoices.Name);
                alterMaster.DropPrimaryKey(PL_SalesInvoices.Name);
                alterMaster.Alter();

                alterMaster.AlterTable(PL_SalesInvoices.Name);
                alterMaster.AlterAddPrimaryKey(PL_SalesInvoices.InvoiceType, PL_SalesInvoices.InvoiceNo);
                alterMaster.Alter();
            }

            if (dbVersion < 10)
            {
                SqlCreateTable alterMaster = new SqlCreateTable(SqlABConnection);

                alterMaster.AlterTable(PL_SalesInvoices.Name);
                alterMaster.AddColumn(PL_SalesInvoices.DeductionAmount);
                alterMaster.Alter();

                var qb = new QueryBuilder(false, true);
                qb.Update(PL_SalesInvoices.Name);
                qb.AddSet<double>(PL_SalesInvoices.DeductionAmount, 0);
                qb.AddCompareIsNull(PL_SalesInvoices.DeductionAmount);
                qb.ExecuteNonQuery();

                alterMaster.AlterTable(PL_SalesInvoices.Name);
                alterMaster.AddColumn(PL_SalesInvoices.BankRestraintAmount);
                alterMaster.Alter();

                var qb1 = new QueryBuilder(false, true);
                qb1.Update(PL_SalesInvoices.Name);
                qb1.AddSet<double>(PL_SalesInvoices.BankRestraintAmount, 0);
                qb1.AddCompareIsNull(PL_SalesInvoices.BankRestraintAmount);
                qb1.ExecuteNonQuery();

                alterMaster.AlterTable(PL_SalesInvoices.Name);
                alterMaster.AddColumn(PL_SalesInvoices.CreditNoteAmount);
                alterMaster.Alter();

                var qb2 = new QueryBuilder(false, true);
                qb2.Update(PL_SalesInvoices.Name);
                qb2.AddSet<double>(PL_SalesInvoices.CreditNoteAmount, 0);
                qb2.AddCompareIsNull(PL_SalesInvoices.CreditNoteAmount);
                qb2.ExecuteNonQuery();

                var update10 = new Library.SalesInvoiceUpdate10();
                update10.UpdateInvoice();
            }
            return true;
        }
    }
}
